package lib.quasar.base.frame;

import android.util.Log;

import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import lib.quasar.base.R;
import lib.quasar.context.BaseApp;
import lib.quasar.context.BaseConstant;
import lib.quasar.db.manager.DBManager;
import lib.quasar.db.table.User;
import lib.quasar.util.CalendarUtil;
import lib.quasar.util.LogUtil;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * description: 解除订阅回调
 * created by kalu on 17-10-16 下午5:07
 */
public interface BasePresenter {

    BaseModel mBaseModel = new BaseModel();

    String NULL_STR = "";
    String ANDROID = "1";
    String USER_PATIENT = "1";
    String USER_CODE = "userCode";
    String USER_ID = "userid";
    String USER_TYPE = "user_type";
    String VERSION = "version";
    String PLATFORM = "platform";
    String DATATYPE = "dataType";
    String CHECK_CODE = "checkCode";
    String MSG_DATAS = "msgDatas";
    String KEY_NAME = "keyName";
    String PATIENT_ID = "patientid";
    String COMMA = ",";
    String MEDIA_TYPE = "application/json; charset=utf-8";
    String RECORD_DATE = "record_date";

    String BEGIN_DATE = "begin_date";
    String END_DATE = "end_date";
    String NUMBER = "number";
    String TYPE = "type";


    String PRESCRIPTION_DATE = "prescription_date";
    String JSON_CONTENT = "json_content";
    String OLD_DATA = "old_data";
    String NEW_DATA = "new_data";
    String JSON_RECORD = "json_record";
    String NULL = "";

    String DOCTOR_ID = "doctorid";
    String DOCTOR_NAME = "doctor_name";
    String SUBMIT_DATETIME = "submit_datetime";

    default HashMap<String, Object> createDatas(String dataType) {

        final HashMap<String, Object> params1 = new HashMap<>();
        params1.clear();
        params1.put(VERSION, BaseConstant.VERSION);
        params1.put(PLATFORM, BaseConstant.PLATFROM);
        params1.put(DATATYPE, dataType);
        params1.put(CHECK_CODE, NULL_STR);
        return params1;
    }

    default RequestBody createParams(Map<String, Object> map, String dataType) {

        final StringBuilder keyName = new StringBuilder();
        final HashMap<String, Object> map2 = new HashMap<>();

        final Long patientId = getPatientId();
        if (null != patientId && !patientId.equals(0L)) {
            map2.put(PATIENT_ID, getPatientId());
            keyName.append(PATIENT_ID);
        }

        if (null != map) {
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                if (!map2.isEmpty()) {
                    keyName.append(COMMA);
                }
                keyName.append(entry.getKey());
                map2.put(entry.getKey(), entry.getValue());
            }
        }

        map2.put(KEY_NAME, keyName.toString());

        final HashMap<String, Object> map1 = createDatas(dataType);
        map1.put(MSG_DATAS, map2);

        final Gson mGson = new Gson();
        final String params = mGson.toJson(map1);
        LogUtil.e("BasePresenter", "createParams ==> " + params);

        return RequestBody.create(MediaType.parse(MEDIA_TYPE), params);
    }

    default <T> RequestBody createParams(T model, String dataType) {

        final HashMap<String, Object> map1 = createDatas(dataType);
        map1.put(MSG_DATAS, model);

        final Gson mGson = new Gson();
        final String params = mGson.toJson(map1);
        LogUtil.e("BasePresenter", "createParams ==> " + params);

        return RequestBody.create(MediaType.parse(MEDIA_TYPE), params);
    }

    default void request(Observable observable, OnModelChangeListener listener) {
        mBaseModel.request(observable, listener);
    }

    default <T> void request(Observable observable, OnModelAcceptChangeListener<T> listener) {
        mBaseModel.request(observable, listener);
    }

    default String getDate() {

        return BaseApp.getContext().getResources().getString(R.string.http_date,
                CalendarUtil.getYears(),
                CalendarUtil.getMonths(),
                CalendarUtil.getDays());
    }

    default String getDate(CharSequence yearMonthDayStr) {

        return BaseApp.getContext().getResources().getString(R.string.http_date_str, yearMonthDayStr);
    }

    default String getDate(CharSequence yearStr, CharSequence monthStr, CharSequence dayStr) {

        return BaseApp.getResource().getString(R.string.http_date, yearStr, monthStr, dayStr);
    }


    default String getDate(String str1, String str2) {

        return BaseApp.getContext().getResources().getString(R.string.http_record_date_time, str1, str2);
    }

    default String getDateLittle() {

        return BaseApp.getContext().getResources().getString(R.string.http_date_little,
                CalendarUtil.getYears(),
                CalendarUtil.getMonths(),
                CalendarUtil.getDays());
    }

    default String getMinute() {

        return BaseApp.getContext().getResources().getString(R.string.http_dates,
                CalendarUtil.getYears(),
                CalendarUtil.getMonths(),
                CalendarUtil.getDays(),
                CalendarUtil.getHours(),
                CalendarUtil.getMinutes());
    }

    default String getDailyMinute(String yearmonthday) {

        return BaseApp.getContext().getResources().getString(R.string.date_daily,
                yearmonthday,
                CalendarUtil.getHours(),
                CalendarUtil.getMinutes());
    }

    default String getMinute3(String yearmonthday) {

        return BaseApp.getContext().getResources().getString(R.string.http_date_minute_yearmonthday,
                yearmonthday,
                CalendarUtil.getHours(),
                CalendarUtil.getMinutes());
    }

    default String getDateTemperature(String temperature1, String temperature2) {

        return BaseApp.getContext().getResources().getString(R.string.date_temperature,
                temperature1, temperature2, CalendarUtil.getSeconds());
    }

    default String getMinute(String hourStr, String minuteStr) {

        return BaseApp.getContext().getResources().getString(R.string.http_dates,
                CalendarUtil.getYears(),
                CalendarUtil.getMonths(),
                CalendarUtil.getDays(),
                hourStr, minuteStr);
    }

    default String getDayMinute(String timeStr) {

        return BaseApp.getContext().getResources().getString(R.string.http_datem,
                CalendarUtil.getYears(),
                CalendarUtil.getMonths(),
                CalendarUtil.getDays(),
                timeStr);
    }


    default String getMonthLittle() {

        return BaseApp.getContext().getResources().getString(R.string.http_month,
                CalendarUtil.getYears(),
                CalendarUtil.getMonths());
    }

    default String getSecond() {

        return BaseApp.getContext().getResources().getString(R.string.http_second,
                CalendarUtil.getYears(),
                CalendarUtil.getMonths(),
                CalendarUtil.getDays(),
                CalendarUtil.getHours(),
                CalendarUtil.getMinutes(),
                CalendarUtil.getSeconds());
    }

    default String getSecondYearMonthDay(String yearmonthday) {

        return BaseApp.getContext().getResources().getString(R.string.http_second_yearmonthday,
                yearmonthday,
                CalendarUtil.getHours(),
                CalendarUtil.getMinutes(),
                CalendarUtil.getSeconds());
    }

    default String getMedicineDate(String hourMinureStr) {
        return BaseApp.getContext().getResources().getString(R.string.http_time,
                CalendarUtil.getYears(),
                CalendarUtil.getMonths(),
                CalendarUtil.getDays(),
                hourMinureStr);
    }

    default String getDateEvent(String yearmonthday) {
        return BaseApp.getContext().getResources().getString(R.string.date_event,
                yearmonthday,
                CalendarUtil.getHours(),
                CalendarUtil.getMinutes());
    }

    default String getHms(String hm) {
        return BaseApp.getContext().getResources().getString(R.string.http_second_little,
                hm,
                "00");
    }

    default String getMinuteLittle() {

        return BaseApp.getContext().getResources().getString(R.string.http_second_little,
                CalendarUtil.getHours(),
                CalendarUtil.getMinutes());
    }

    default Long getPatientId() {

        try {
            final User doctorModel = DBManager.getInstance().syncGetUserModel();
            return doctorModel.getPatientid();
        } catch (Exception e) {
            Log.e("BasePresenter", "getPatientId ==> " + e.getMessage(), e);
            return 0L;
        }
    }

    default String getClientId() {

        try {
            final User doctorModel = DBManager.getInstance().syncGetUserModel();
            return doctorModel.getClientid();
        } catch (Exception e) {
            Log.e("BasePresenter", "getClientId ==> " + e.getMessage(), e);
            return "";
        }
    }

    default String getPatientName() {

        try {
            final User doctorModel = DBManager.getInstance().syncGetUserModel();
            return doctorModel.getReal_name();
        } catch (Exception e) {
            Log.e("BasePresenter", "getPatientName ==> " + e.getMessage(), e);
            return "";
        }
    }

    default String getHospitalName() {

        try {
            final User doctorModel = DBManager.getInstance().syncGetUserModel();
            return doctorModel.getHospital_name();
        } catch (Exception e) {
            Log.e("BasePresenter", "getHospitalName ==> " + e.getMessage(), e);
            return "";
        }
    }

    default String getCalendar() {
        return CalendarUtil.getYears() + "-" + CalendarUtil.getMonths() + "-" + CalendarUtil.getDays();
    }

    default Boolean needGuide() {

        try {

            final User user = DBManager.getInstance().syncGetUserModel();
            return user.needGuide();

        } catch (Exception e) {
            Log.e("BasePresenter", "needGuide ==> " + e.getMessage(), e);
            return true;
        }
    }

    default Boolean needLogin() {

        try {

            final User user = DBManager.getInstance().syncGetUserModel();
            return user.needLogin();

        } catch (Exception e) {
            Log.e("BasePresenter", "needGuide ==> " + e.getMessage(), e);
            return true;
        }
    }

    void recycler();
}